/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 */
package com.googlecode.ivybeans.module;

import java.io.File;
import org.netbeans.api.project.Project;
import org.netbeans.spi.project.AuxiliaryConfiguration;
import org.openide.xml.XMLUtil;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

/**
 * 
 * @author lforet
 */
public class IvyConfigurationHelper {

    public static final String PROJECT_CONFIGURATION_NAMESPACE = "http://code.google.com/p/ivybeans/ns/ivy-module/1"; // NOI18N
    public static final String IVY_CONFIGURATION_ELEMENT_NAME = "ivyConfiguration"; //NO I18N
    public static final String DESCRIPTOR_FILE_ELEMENT_NAME = "descriptorFile"; //NO I18N
    public static final String SETTINGS_FILE_ELEMENT_NAME = "settingsFile"; //NO I18N
    private static IvyConfigurationHelper instance;

    private IvyConfigurationHelper() {
    }

    public static IvyConfigurationHelper getInstance() {
        if (instance == null) {
            instance = new IvyConfigurationHelper();
        }
        return instance;
    }

    public IvyLibraryController initController(Project project, AuxiliaryConfiguration auxiliaryConfiguration) {
        boolean enabled = false;
        File descriptorFile =null;
        File settingsFile = null;
        Element ivyConfElt = auxiliaryConfiguration.getConfigurationFragment(IVY_CONFIGURATION_ELEMENT_NAME, PROJECT_CONFIGURATION_NAMESPACE, true);
        if (ivyConfElt != null && ivyConfElt.hasChildNodes()) {
            enabled = true;

            Element descriptorFileElt = (Element) ivyConfElt.getElementsByTagName(DESCRIPTOR_FILE_ELEMENT_NAME).item(0);
            if (descriptorFileElt != null) {
                String descriptorFilename = descriptorFileElt.getTextContent();
                descriptorFile = FileHelper.getInstance().getFile(project.getProjectDirectory(), descriptorFilename);
            }

            Element settingsFileElt = (Element) ivyConfElt.getElementsByTagName(SETTINGS_FILE_ELEMENT_NAME).item(0);
            if (settingsFileElt != null) {
                String settingsFilename = settingsFileElt.getTextContent();
                settingsFile = FileHelper.getInstance().getFile(project.getProjectDirectory(), settingsFilename);
            }
        }
        return new IvyLibraryController(project, enabled, descriptorFile, settingsFile);
    }

    public void clean(Project project) {
        AuxiliaryConfiguration auxiliaryConfiguration = (AuxiliaryConfiguration) project.getLookup().lookup(AuxiliaryConfiguration.class);
        assert auxiliaryConfiguration != null;
        auxiliaryConfiguration.removeConfigurationFragment(IVY_CONFIGURATION_ELEMENT_NAME, PROJECT_CONFIGURATION_NAMESPACE, true);
    }

    public void store(Project project, File descriptorFile, File settingsFile) {
        clean(project);
        if (descriptorFile == null) {
            return;
        }
        AuxiliaryConfiguration auxiliaryConfiguration = (AuxiliaryConfiguration) project.getLookup().lookup(AuxiliaryConfiguration.class);
        Document doc = XMLUtil.createDocument("whatever", null, null, null);
        Element fragment = doc.createElementNS(PROJECT_CONFIGURATION_NAMESPACE, IVY_CONFIGURATION_ELEMENT_NAME);
        Element descriptorFileElement = doc.createElement(DESCRIPTOR_FILE_ELEMENT_NAME);
        fragment.appendChild(descriptorFileElement);
        String descriptorPath = FileHelper.getInstance().getPath(project.getProjectDirectory(), descriptorFile);
        descriptorFileElement.appendChild(doc.createTextNode(descriptorPath));
        if (settingsFile != null) {
            Element settingsFileElement = doc.createElement(SETTINGS_FILE_ELEMENT_NAME);
            fragment.appendChild(settingsFileElement);
            String settingsPath = FileHelper.getInstance().getPath(project.getProjectDirectory(), settingsFile);
            settingsFileElement.appendChild(doc.createTextNode(settingsPath));
        }

        auxiliaryConfiguration.putConfigurationFragment(fragment, true);
    }    
}
